package com.tom.demo.common.config;

import com.tom.demo.service.thirdparty.ThirdPartyGetTokenService;
import okhttp3.Interceptor;
import okhttp3.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 为服务配置提供认证拦截器
 */
@Configuration
public class RetrofitAuthConfig {
    @Resource
    private ThirdPartyGetTokenService thirdPartyGetTokenService;
    /**
     * 《聚合数据平台》<a href="https://www.juhe.cn">...</a>
     * (name = "newsHeadlineApiAuthInterceptor") 对应配置文件yml中的 interceptors.bean
     * 获取新闻头条
     * @return Interceptor
     */
    @Bean(name = "newsHeadlineApiAuthInterceptor")
    public Interceptor newsHeadlineApiAuthInterceptor() {
        return chain -> {
            //String token = thirdPartyGetTokenService.getTokenForAService();
            //String sessionId = cookieService.getSessionIdForServiceB();
            Request request = chain.request().newBuilder()
                    .addHeader("Accept", "application/json")
                    //.header("Authorization", "Bearer " + token)
                    //.header("Cookie", "SESSIONID=" + sessionId)
                    .build();
            return chain.proceed(request);
        };
    }

    /**
     * 《聚合数据平台》<a href="https://www.juhe.cn">...</a>
     * (name = "urbanAirQualityApiAuthInterceptor") 对应配置文件yml中的 interceptors.bean
     * 获取城市空气质量
     * @return Interceptor
     */
    @Bean(name = "urbanAirQualityApiAuthInterceptor")
    public Interceptor urbanAirQualityApiAuthInterceptor() {
        return chain -> {
            //String token = thirdPartyGetTokenService.getTokenForAService();
            //String sessionId = cookieService.getSessionIdForServiceB();
            Request request = chain.request().newBuilder()
                    .addHeader("Accept", "application/json")
                    //.header("Authorization", "Bearer " + token)
                    //.header("Cookie", "SESSIONID=" + sessionId)
                    .build();
            return chain.proceed(request);
        };
    }
}